package com.agv.web.security;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

import com.agv.core.exception.SecurityException;
import com.agv.core.objectDomain.Usuario;
import com.agv.core.services.ISecurityService;

/**
 * MyEclipse Struts Creation date: 05-31-2011
 * 
 * XDoclet definition:
 * 
 * @struts.action path="/agvWebLogin" name="loginForm" input="/pages/login.jsp"
 *                scope="request" validate="true"
 */
public class LoginAction extends DispatchAction {

	private ISecurityService securityService;

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		LoginForm loginForm = (LoginForm) form;
		try {
			if (loginForm.getCdUsuario().equals("")
					|| loginForm.getPwContra().equals("")) {
				ActionMessages messages = new ActionMessages();
				ActionMessage msg = new ActionMessage(
						"agv.adm.sec.campoRequerido");
				messages.add("message", msg);
				saveMessages(request, messages);
				return mapping.findForward("login");
			}

			Usuario usuario = securityService.login(loginForm.getCdUsuario(),
					loginForm.getPwContra());
			if (usuario.isCdPrimerAcceso()) {
				request.getSession().setAttribute("primerAcceso", "si");
				request.getSession().setAttribute("usuarioObj", usuario);
				request.getSession().setAttribute("perfilObj",
						usuario.getPerfil());
				return mapping.findForward("login");
			}

			request.getSession().removeAttribute("catCompleto");
			request.getSession().removeAttribute("catInsertado");
			request.getSession().removeAttribute("catalogoObj");
			request.getSession().removeAttribute("altaCatalogo");
			request.getSession().removeAttribute("pasoActual");
			request.getSession().removeAttribute("totalPasos");
			request.getSession().removeAttribute("buscaCatalogo");
			request.getSession().removeAttribute("actualizarCatalogo");
			request.getSession().removeAttribute("modulo");
			request.getSession().removeAttribute("subModulo");
			request.getSession().removeAttribute("funcion");

			request.getSession().setAttribute("ruta", "");
			request.getSession().setAttribute("usuarioObj", usuario);
			request.getSession().setAttribute("perfilObj", usuario.getPerfil());
		} catch (SecurityException e) {
			ActionMessages messages = new ActionMessages();
			ActionMessage msg = new ActionMessage("agv.adm.sec.incorrecto");
			messages.add("message", msg);
			saveMessages(request, messages);
			return mapping.findForward("login");
		}
		return mapping.findForward("inicio");
	}

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward primerInicio(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		LoginForm loginForm = (LoginForm) form;
		try {
			if (loginForm.getCdAcceso().equals("")
					|| loginForm.getPwContra().equals("")) {
				ActionMessages messages = new ActionMessages();
				ActionMessage msg = new ActionMessage(
						"agv.adm.sec.campoRequerido");
				messages.add("message", msg);
				saveMessages(request, messages);
				return mapping.findForward("login");
			}

			Usuario usuario = (Usuario) request.getSession().getAttribute(
					"usuarioObj");
			securityService.validaPrimerAcceso(loginForm.getCdAcceso(),
					loginForm.getPwContra(), usuario.getIdUsuario());

		} catch (SecurityException e) {
			ActionMessages messages = new ActionMessages();
			ActionMessage msg = new ActionMessage(
					"agv.adm.sec.cdacceso.incorrecto");
			messages.add("message", msg);
			saveMessages(request, messages);
			return mapping.findForward("login");
		}
		return mapping.findForward("inicio");
	}

	public void setSecurityService(ISecurityService securityService) {
		this.securityService = securityService;
	}
}